package cn.zzszxyy.hr.service;

import cn.zzszxyy.hr.model.DeptTransferEntity;
import cn.zzszxyy.hr.repository.DeptTransferRepository;
import cn.zzszxyy.hr.repository.EmployeeRepository;
import cn.zzszxyy.hr.viewmodel.DeptTransView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-29
 * 说明:部门调动
 */
@Service
public class DeptTransferService {

    @Autowired
    private DeptTransferRepository deptTransferRepository;

    @Autowired
    private EmployeeRepository employeeRepository;

    public List<DeptTransferEntity> findAll() {
        return deptTransferRepository.findAll();
    }

    public Page<DeptTransferEntity> findAllWithPage(int pageIndex, int pageSize) {
        PageRequest pageRequest = new PageRequest(--pageIndex, pageSize);
        Page<DeptTransferEntity> transList = deptTransferRepository.findAll(pageRequest);
        return transList;
    }

//    public Page<DeptTransferEntity> findByDeptNameLike(String deptName, int pageIndex, int pageSize){
//        PageRequest pageRequest = new PageRequest(--pageIndex, pageSize);
//        return deptTransferRepository.findByDeptNameLike(deptName, pageRequest);
//    }

    public DeptTransferEntity findOne(int id) {
        return deptTransferRepository.findOne(id);
    }

    @Transactional
    public DeptTransferEntity save(DeptTransferEntity deptTransferEntity) {
        //修改当前人员所在部门
        employeeRepository.updateCurDeptById(deptTransferEntity.getToDept(), deptTransferEntity.getEmployeeId());
        //增加部门调动记录
        return deptTransferRepository.save(deptTransferEntity);
    }

    @Transactional
    public void delete(int id) {
        //删除：本表记录
        deptTransferRepository.delete(id);
    }

    public Page<DeptTransView> findDeptTransViewByDept(Integer deptId, int pageIndex, int pageSize) {
        PageRequest pageRequest = new PageRequest(--pageIndex, pageSize);
        return deptTransferRepository.findDeptTransViewByDept(deptId, pageRequest);
    }


    public Page<DeptTransView> findDeptTransView(int pageIndex, int pageSize) {
        PageRequest pageRequest = new PageRequest(--pageIndex, pageSize);
        return deptTransferRepository.findDeptTransView(pageRequest);
    }

    public List<DeptTransView> findByEmployeeId(Integer employeeId) {
        return deptTransferRepository.findByEmployeeId(employeeId);
    }

}
